/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Properties;
using ELE.EnterpriseLibrary.Data.Configuration.Design.Properties;

namespace ELE.EnterpriseLibrary.Data.Configuration.Design
{
	public class ObjectMappingSettingsBuilder 
	{
		private ObjectMappingSettingsNode objectMappingSettingsNode;
		private IConfigurationUIHierarchy hierarchy;
		private ObjectMappingSettings objectMappingConfiguration;

		public ObjectMappingSettingsBuilder(IServiceProvider serviceProvider, ObjectMappingSettingsNode objectMappingSettingsNode) 
		{
			this.objectMappingSettingsNode = objectMappingSettingsNode;
			hierarchy = ServiceHelper.GetCurrentHierarchy(serviceProvider);
			objectMappingConfiguration = new ObjectMappingSettings();
		}

		public ObjectMappingSettings Build()
		{
      BuildObjectTypes();
      BuildObjectQueryBuilders();
      BuildObjectSqlMappingProviders();
      BuildSqlQueries();

			objectMappingConfiguration.DefaultQuery = objectMappingSettingsNode.DefaultSqlQuery.Name;
      objectMappingConfiguration.DefaultMapping = objectMappingSettingsNode.DefaultObjectSqlMapping.Name;


			return objectMappingConfiguration;
		}

		private void BuildSqlQueries()
		{
			foreach (SqlQueryNode managerNode in hierarchy.FindNodesByType(objectMappingSettingsNode, typeof(SqlQueryNode)))
			{
				SqlQueryData sqlQueryData = managerNode.SqlQueryData;

				objectMappingConfiguration.Queries.Add(sqlQueryData);
			}
		}

		private void BuildObjectSqlMappingProviders()
		{
			foreach (ObjectSqlMappingNode objectSqlMappingNode in hierarchy.FindNodesByType(objectMappingSettingsNode, typeof(ObjectSqlMappingNode)))
			{
				MappingQueryData objectSqlMappingData = objectSqlMappingNode.ObjectMappingData;

				objectMappingConfiguration.Mappings.Add(objectSqlMappingData);
			}
    }

    private void BuildObjectQueryBuilders()
    {
      foreach (QueryBuilderNode node in hierarchy.FindNodesByType(objectMappingSettingsNode, typeof(QueryBuilderNode)))
      {
        QueryBuilderData data = node.QueryBuilderData;

        objectMappingConfiguration.QueryBuilders.Add(data);
      }
    }

    private void BuildObjectTypes()
    {
      ObjectQueryData data = new ObjectQueryData();
      foreach (ObjectTypeAssemblyNode node in hierarchy.FindNodesByType(objectMappingSettingsNode, typeof(ObjectTypeAssemblyNode)))
      {
        data.Assemblies.Add(node.AssemblyData);
      }
      foreach (ObjectTypeDefNode node in hierarchy.FindNodesByType(objectMappingSettingsNode, typeof(ObjectTypeDefNode)))
      {
        data.Types.Add(node.TypeData);
      }
      objectMappingConfiguration.Objects = data;
    }
	}
}
